package com.zenithmind.exam.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zenithmind.exam.pojo.entity.Exam;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.math.BigDecimal;
import java.util.List;

/**
 * 考试Mapper接口
 * 
 * @author ZenithMind
 * @since 2024-07-01
 */
@Mapper
public interface ExamMapper extends BaseMapper<Exam> {

    /**
     * 根据分类ID查询考试列表
     * 
     * @param categoryId 分类ID
     * @return 考试列表
     */
    @Select("SELECT * FROM exam WHERE category_id = #{categoryId} AND is_deleted = 0 ORDER BY create_time DESC")
    List<Exam> selectByCategoryId(@Param("categoryId") String categoryId);

    /**
     * 根据创建者ID查询考试列表
     * 
     * @param creatorId 创建者ID
     * @return 考试列表
     */
    @Select("SELECT * FROM exam WHERE creator_id = #{creatorId} AND is_deleted = 0 ORDER BY create_time DESC")
    List<Exam> selectByCreatorId(@Param("creatorId") String creatorId);

    /**
     * 根据状态查询考试列表
     * 
     * @param status 状态
     * @return 考试列表
     */
    @Select("SELECT * FROM exam WHERE status = #{status} AND is_deleted = 0 ORDER BY create_time DESC")
    List<Exam> selectByStatus(@Param("status") Integer status);

    /**
     * 查询进行中的考试列表
     * 
     * @return 进行中的考试列表
     */
    @Select("SELECT * FROM exam WHERE status = 2 AND start_time <= NOW() AND end_time >= NOW() AND is_deleted = 0 ORDER BY start_time ASC")
    List<Exam> selectOngoingExams();

    /**
     * 查询即将开始的考试列表
     * 
     * @return 即将开始的考试列表
     */
    @Select("SELECT * FROM exam WHERE status = 1 AND start_time > NOW() AND is_deleted = 0 ORDER BY start_time ASC")
    List<Exam> selectUpcomingExams();

    /**
     * 分页查询考试列表
     * 
     * @param page 分页参数
     * @param title 考试标题（模糊查询）
     * @param categoryId 分类ID
     * @param creatorId 创建者ID
     * @param type 考试类型
     * @param status 状态
     * @return 分页结果
     */
    IPage<Exam> selectExamPage(Page<Exam> page,
                              @Param("title") String title,
                              @Param("categoryId") String categoryId,
                              @Param("creatorId") String creatorId,
                              @Param("type") Integer type,
                              @Param("status") Integer status);

    /**
     * 更新考试参与人数
     * 
     * @param examId 考试ID
     * @return 更新行数
     */
    @Update("UPDATE exam SET participant_count = participant_count + 1, update_time = NOW() WHERE id = #{examId}")
    int incrementParticipantCount(@Param("examId") String examId);

    /**
     * 更新考试提交人数
     * 
     * @param examId 考试ID
     * @return 更新行数
     */
    @Update("UPDATE exam SET submitted_count = submitted_count + 1, update_time = NOW() WHERE id = #{examId}")
    int incrementSubmittedCount(@Param("examId") String examId);

    /**
     * 更新考试统计信息
     * 
     * @param examId 考试ID
     * @param avgScore 平均分
     * @param maxScore 最高分
     * @param minScore 最低分
     * @param passRate 通过率
     * @return 更新行数
     */
    @Update("UPDATE exam SET avg_score = #{avgScore}, max_score = #{maxScore}, min_score = #{minScore}, pass_rate = #{passRate}, update_time = NOW() WHERE id = #{examId}")
    int updateStatistics(@Param("examId") String examId,
                        @Param("avgScore") BigDecimal avgScore,
                        @Param("maxScore") BigDecimal maxScore,
                        @Param("minScore") BigDecimal minScore,
                        @Param("passRate") BigDecimal passRate);

    /**
     * 更新考试状态
     * 
     * @param examId 考试ID
     * @param status 状态
     * @return 更新行数
     */
    @Update("UPDATE exam SET status = #{status}, update_time = NOW() WHERE id = #{examId}")
    int updateStatus(@Param("examId") String examId, @Param("status") Integer status);

    /**
     * 统计用户创建的考试数量
     * 
     * @param creatorId 创建者ID
     * @return 考试数量
     */
    @Select("SELECT COUNT(*) FROM exam WHERE creator_id = #{creatorId} AND is_deleted = 0")
    Integer countByCreatorId(@Param("creatorId") String creatorId);

    /**
     * 查询热门考试列表
     * 
     * @param limit 限制数量
     * @return 热门考试列表
     */
    @Select("SELECT * FROM exam WHERE status IN (1, 2) AND is_deleted = 0 ORDER BY participant_count DESC LIMIT #{limit}")
    List<Exam> selectHotExams(@Param("limit") Integer limit);

    /**
     * 查询需要自动结束的考试
     * 
     * @return 需要自动结束的考试列表
     */
    @Select("SELECT * FROM exam WHERE status = 2 AND end_time < NOW() AND is_deleted = 0")
    List<Exam> selectExamsToEnd();

    /**
     * 查询需要自动开始的考试
     * 
     * @return 需要自动开始的考试列表
     */
    @Select("SELECT * FROM exam WHERE status = 1 AND start_time <= NOW() AND end_time > NOW() AND is_deleted = 0")
    List<Exam> selectExamsToStart();
}
